package org.example.setup.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("qrCode")
public class QrCoreController {

    @PostMapping("qrCode")
    public File qrCore(MultipartFile file){
        try {
            Map map = new HashMap();
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            map.put(EncodeHintType.MARGIN, 1);
            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix bitMatrix = writer.encode("当这世界已经准备将我遗弃", BarcodeFormat.QR_CODE, 300, 300, map);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();

            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for(int x = 0; x < width; x++){
                for(int y = 0; y < height; y++){
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                }
            }

            InputStream inputStream = file.getInputStream();
            Image logoImage = ImageIO.read(inputStream);
            int logoWidth = logoImage.getWidth(null);
            int logoHeight = logoImage.getHeight(null);
            if(logoWidth>60){
                logoWidth = 60;
            }
            if (logoHeight>60){
                logoHeight = 60;
            }

            Image scaledLogo = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);

            Graphics2D graphics2D = image.createGraphics();
            int x=(300-logoWidth)/2;
            int y=(300-logoHeight)/2;
            graphics2D.drawImage(scaledLogo, x, y, null);
            Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 15, 10);
            graphics2D.setStroke(new BasicStroke(4f));
            graphics2D.draw(shape);
            graphics2D.dispose();

            File outputfile = new File("D:\\secFile\\logo.jpg");
            ImageIO.write(image, "jpg", outputfile);

            return new File("D:\\secFile\\logo.jpg");

        } catch (WriterException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
